﻿using System;
using System.Net;
using System.Threading.Tasks;
using OpenFileTransfer.Wpf.Models;
using OpenFileTransfer.Wpf.Services;
using SuperSocket.Channel;
using SuperSocket.WebSocket.Server;

namespace OpenFileTransfer.Wpf.WebSocketServer.Server
{
    public class MySession : WebSocketSession
    {
        public SessionInfo Info { get; }
        public string IpString => $"{(RemoteEndPoint as IPEndPoint)?.Address}";

        private readonly ILogService _logService;

        public MySession(ILogService logService)
        {
            _logService = logService;
            Info = new SessionInfo();
        }

        protected override async ValueTask OnSessionConnectedAsync()
        {
            _logService.Info($@"New Session connected: {RemoteEndPoint}.");
            Info.LastActiveTime = DateTime.Now;
            await base.OnSessionConnectedAsync();
        }

        protected override async ValueTask OnSessionClosedAsync(CloseEventArgs e)
        {
            _logService.Info($@"Session {RemoteEndPoint} closed: {e.Reason}.");
            await base.OnSessionClosedAsync(e);
        }

        public void DoHeartBeat()
        {
            Info.LastActiveTime = DateTime.Now;
        }
    }
}
